<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人信息页面</title>
    <link href="../css/personalInfo.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div style="width: 100%;height:800px;position: center;background-color: #dddddd;">
    <div class="div" style="margin-top: 10px">
        <label class="label">用户组：</label>
        <select id="groupid" autofocus="autofocus" class="input">
            <option value="请选择">请选择</option>
        </select>
    </div>
    <div class="div" style="margin-top: 10px">
        <label class="label">用户名：</label><input class="input" id="userName" name="userName" type="text" placeholder="请输入用户名">
    </div>
    <!--<div class="div">
        <label class="label">密  码：</label><input class="input" id="password" name="password" type="password">
    </div>-->
    <div class="div">
        <label class="label">姓  名：</label><input class="input" id="realName" name="realName" type="text" placeholder="请输入真实姓名">
    </div>
    <div class="div">
        <label class="label">身份证：</label><input class="input" id="identity" name="identity" type="text" placeholder="请输入身份证号码">
    </div>
    <div class="div">
        <label class="label">联系电话：</label><input class="input" id="phone" name="phone" type="text" placeholder="请输入联系电话">
    </div>
    <div class="div">
        <label class="label">备  注：</label><input class="input" id="remark" name="remark" type="text" placeholder="请输入备注信息">
    </div>
    <div class="div"><input class="btn" type="submit" id="submit" name="submit" value="编辑" onclick="editUser()"></div>
</div>

<script type="text/javascript" src="../lib/jquery.min.js"></script>
<script type="text/javascript" src="../lib/H-ui.min.js"></script>
<script type="text/javascript">

    var id = $.cookie('uidCookie');
    function userGroup(){
        var usergroup=null;
        $.ajaxSetup({async : false});
        $.ajax({
            type: "get",
            url: "/enum/groupEnum",
            data: {
            },
            dataType: "json",
            success: function (data) {
                usergroup=data;
            },
            fail:function (data) {

                alert(JSON.stringify(data));
            },
            error: function(xhr) {
                alert('Error' + JSON.stringify(xhr));
            }
        });
        return usergroup;
    }

    //页面加载处理
    $(document).ready(function () {


        $.ajax({
            type: "post",
            url: "/user/selectOne",
            data: {
                id: id
            },
            dataType: "json",
            success: function (data) {
                var status=data.status;
                var msg=data.msg;
                var list=data.data;
                console.log(data);

                if(status == 0){

                    var groupid = list.groupid;
                    var username = list.username;
                    var realname = list.realname;
                    var identity = list.identity;
                    var phone = list.phone;
                    var remark=list.remark;

                    var html = "";
                    var groups=userGroup();
                    for(var i=0;i<groups.length;i++){
                        var row=groups[i];
                        if( row.groupId == groupid){
                            var rowHtml="<option value=\""+row.groupId+"\" selected>"+row.desc+"</option>";
                        }else{
                            var rowHtml="<option value=\""+row.groupId+"\">"+row.desc+"</option>";
                        }

                        html=html+rowHtml;
                    }

                    $("#groupid").append(html);
                    $("#userName").val(username);
                    $("#realName").val(realname);
                    $("#identity").val(identity);
                    $("#phone").val(phone);
                    $("#remark").val(remark);

                }else{
                    alert(msg);
                }

            }
        });


    });

    //编辑用户
    function editUser(){
        var groupid = $("#groupid").val();
        var identity = $("#identity").val();
        var realname = $("#realName").val();
        var username = $("#userName").val();
        var phone = $("#phone").val();
        var remark = $("#remark").val();

        if(groupid == null || groupid == ""){
            alert("请选择用户组！");
            return;
        }else if(identity == null || identity == ""){
            alert("身份证不能为空！");
            return;
        }else if(realname == null || realname == ""){
            alert("姓名不能为空！");
            return;
        }else if(username == null || username == ""){
            alert("用户名不能为空！");
            return;
        }else{
            $.ajax({
                type: 'POST',
                url: '/user/updateOne',
                data:{
                    id:id,
                    groupid:groupid,
                    username:username,
                    identity:identity,
                    realname:realname,
                    phone:phone,
                    remark:remark
                },
                dataType: 'json',
                success: function(data){
                    var status=data.status;
                    var msg=data.msg;
                    var list=data.data;

                    if(status == 0){
                        alert("更新成功！");
                        window.parent.location.href = "admin-index.html";
                    }else{
                        alert(msg);
                    }

                },
                error:function(xhr) {
                    alert('Error' + JSON.stringify(xhr));
                },
            });
        }

    }



</script>

</body>
</html>